<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :label="item.username"
          :value="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="doAdd">确定</el-button>
      <el-button size="small">取消</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
// 导入员工简单信息列表,添加部门,编辑文件
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default ({
  props: {
    // 用户id
    pid: {
      type: String,
      required: true
    },
    // 用户详细信息
    isedit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      // 弹出框数据
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 存储员工简单信息列表
      list: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 进入页面调用
  async created() {
    const { data: res } = await getEmployeeSimple()
    this.list = res
    if (this.isedit) {
      const res = await getDepartDetail(this.pid)
      console.log(res)
      this.form = res.data
    } else {
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    }
  },
  methods: {
    // 点击确定添加,或编辑人员
    doAdd() {
      // 表单全局验证
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.doEdit()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 提交
    async doEdit() {
      if (!this.isedit) {
        const res = await addDepartments({ ...this.form, pid: this.pid })
        if (res.code === 10000) {
          this.$message.success(res.message)
        }
      } else {
        const res = await updateDepartments(this.pid, this.form)
        this.$message.success('编辑成功')
        console.log(res)
      }
      // 新增数据成功 提交给父组件
      this.$emit('reset')
    }
  }
})
</script>
